package com.neu.api.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.api.constan.StatusConst;
import com.neu.api.dto.UserDTO;
import com.neu.api.mapper.UserMapper;
import com.neu.api.pojo.User;
import com.neu.api.query.UserQuery;
import com.neu.api.service.UserService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Value("${neu.password}")
    private String password;
    //private String password = "123456";

    @Resource
    private UserMapper userMapper;

    /**
     * 查询用户
     * @return
     */
    @Override
    public PageResult<UserVO> find(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPage(),userQuery.getPageSize());  //告诉PageHelper此处需要分页

        Page<UserVO> page = userMapper.find(userQuery);

        PageInfo<UserVO> pageInfo = page.toPageInfo();

        List<UserVO> userVOList = pageInfo.getList();

        long total = pageInfo.getTotal();

        PageResult<UserVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(userVOList);
        return pageResult;
        //查询语句
        ////        userQuery.setJump((userQuery.getPage()-1)*userQuery.getPageSize());
        ////        List<UserVO> userVOList = userMapper.find(userQuery);
        ////
        ////        long total = userMapper.findCount(userQuery);
        ////
        ////        PageResult<UserVO> page = new PageResult();
        ////        page.setList(userVOList);
        ////        page.setTotal(total);
        ////        return page;
        //        //return userMapper.find(userQuery);我写的
    }

    /**
     * 添加用户
     * @param userDTO
     */
    @Override
    public void add(UserDTO userDTO) {

        //步骤
            //第一:先把dto--->pojo
            //第二:调用mapper/dao执行insert 语句


        //方式一:将dto-->pojo
        /*
        User user = new User();
        user.setName(userDTO.getName());
        user.setPassword(userDTO.getPassword());
        */

        //方式二:将dto-->pojo
//        User user = new User();
//        BeanUtils.copyProperties(userDTO,user);

        User user = new User();  //name loginName phone
        BeanUtils.copyProperties(userDTO,user);

        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        user.setStatus(StatusConst.ACTIVE_STATUS);

        userMapper.add(user);



    }

    /**
     * 用户登录
     * @param loginName
     * @param password
     * @return
     */
    @Override
    public UserVO login(@Param("loginName")String loginName, @Param("password")String password) {
        //密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        return userMapper.login(loginName,password);
    }

    @Override
    public void update(Long id, UserDTO userDTO) {
        User user = new User();

        BeanUtils.copyProperties(userDTO,user);

        user.setUpdateTime(new Date());
        user.setId(id);
        userMapper.update(user);
    }



    @Override
    public void deleteById(long id) {
        userMapper.deleteById(id);
    }

    /**
     * 启用禁用账号
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(long id, byte status) {
//        User user = new User();
//        user.setId(id);
//        byte nowStatus;
//        if(StatusConst.ACTIVE_STATUS == status){
//            user.setStatus(StatusConst.ACTIVE_STATUS);
//            userMapper.update(user);;
//            nowStatus = StatusConst.ACTIVE_STATUS;
//        }else if(StatusConst.INACTIVE_STATUS == status){
//            userMapper.update(user);
//            user.setStatus(StatusConst.INACTIVE_STATUS);
//            nowStatus = StatusConst.INACTIVE_STATUS;
//        }else {
//            throw new ParamException("参数错误!");
//        }
        User user = new User();
        user.setId(id);
        user.setUpdateTime(new Date());
        user.setStatus(status);
        userMapper.update(user);
    }

}
